/*
 ***********************************************************************
 * This code sample is offered under a modified BSD license.           *
 * Copyright (C) 2010, Motorola, Inc. All rights reserved.             *
 * For more details, see MOTODEV_Studio_for_Android_LicenseNotices.pdf * 
 * in your installation folder.                                        *
 ***********************************************************************
 */
package com.like.providers;

import java.util.*;

import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.net.*;
import android.text.*;

import com.like.util.*;

public class TODOSContentProvider extends ContentProvider {

	private DbHelper dbHelper;
	private static HashMap<String, String> TODOS_PROJECTION_MAP;
	private static final String TABLE_NAME = "todos";
	private static final String AUTHORITY = "com.like.providers.todoscontentprovider";

	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/" + TABLE_NAME);
	public static final Uri _ID_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase());
	public static final Uri TITLE_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/title");
	public static final Uri IMPORTANCE_FIELD_CONTENT_URI = Uri
			.parse("content://" + AUTHORITY + "/" + TABLE_NAME.toLowerCase()
					+ "/importance");
	public static final Uri DEADLINE_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/deadline");
	public static final Uri NOTES_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/notes");
	public static final Uri CREATED_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/created");
	public static final Uri FINISH_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/finish");

	public static final String DEFAULT_SORT_ORDER = "_ID ASC";

	private static final UriMatcher URL_MATCHER;

	private static final int TODOS = 1;
	private static final int TODOS__ID = 2;
	private static final int TODOS_TITLE = 3;
	private static final int TODOS_IMPORTANCE = 4;
	private static final int TODOS_DEADLINE = 5;
	private static final int TODOS_NOTES = 6;
	private static final int TODOS_CREATED = 7;
	private static final int TODOS_FINISH = 8;

	// Content values keys (using column names)
	public static final String _ID = "_ID";
	public static final String TITLE = "TITLE";
	public static final String IMPORTANCE = "IMPORTANCE";
	public static final String DEADLINE = "DEADLINE";
	public static final String NOTES = "NOTES";
	public static final String CREATED = "CREATED";
	public static final String FINISH = "FINISH";

	public boolean onCreate() {
		dbHelper = new DbHelper(getContext(), true);
		return (dbHelper == null) ? false : true;
	}

	public Cursor query(Uri url, String[] projection, String selection,
			String[] selectionArgs, String sort) {
		SQLiteDatabase mDB = dbHelper.getReadableDatabase();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		switch (URL_MATCHER.match(url)) {
		case TODOS:
			qb.setTables(TABLE_NAME);
			qb.setProjectionMap(TODOS_PROJECTION_MAP);
			break;
		case TODOS__ID:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("_id=" + url.getPathSegments().get(1));
			break;
		case TODOS_TITLE:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("title='" + url.getPathSegments().get(2) + "'");
			break;
		case TODOS_IMPORTANCE:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("importance='" + url.getPathSegments().get(2) + "'");
			break;
		case TODOS_DEADLINE:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("deadline='" + url.getPathSegments().get(2) + "'");
			break;
		case TODOS_NOTES:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("notes='" + url.getPathSegments().get(2) + "'");
			break;
		case TODOS_CREATED:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("created='" + url.getPathSegments().get(2) + "'");
			break;
		case TODOS_FINISH:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("finish='" + url.getPathSegments().get(2) + "'");
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
		String orderBy = "";
		if (TextUtils.isEmpty(sort)) {
			orderBy = DEFAULT_SORT_ORDER;
		} else {
			orderBy = sort;
		}
		Cursor c = qb.query(mDB, projection, selection, selectionArgs, null,
				null, orderBy);
		c.setNotificationUri(getContext().getContentResolver(), url);
		return c;
	}

	public String getType(Uri url) {
		switch (URL_MATCHER.match(url)) {
		case TODOS:
			return "vnd.android.cursor.dir/vnd.com.like.providers.todos";
		case TODOS__ID:
			return "vnd.android.cursor.item/vnd.com.like.providers.todos";
		case TODOS_TITLE:
			return "vnd.android.cursor.item/vnd.com.like.providers.todos";
		case TODOS_IMPORTANCE:
			return "vnd.android.cursor.item/vnd.com.like.providers.todos";
		case TODOS_DEADLINE:
			return "vnd.android.cursor.item/vnd.com.like.providers.todos";
		case TODOS_NOTES:
			return "vnd.android.cursor.item/vnd.com.like.providers.todos";
		case TODOS_CREATED:
			return "vnd.android.cursor.item/vnd.com.like.providers.todos";
		case TODOS_FINISH:
			return "vnd.android.cursor.item/vnd.com.like.providers.todos";

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
	}

	public Uri insert(Uri url, ContentValues initialValues) {
		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
		long rowID;
		ContentValues values;
		if (initialValues != null) {
			values = new ContentValues(initialValues);
		} else {
			values = new ContentValues();
		}
		if (URL_MATCHER.match(url) != TODOS) {
			throw new IllegalArgumentException("Unknown URL " + url);
		}

		rowID = mDB.insert("todos", "todos", values);
		if (rowID > 0) {
			Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(uri, null);
			return uri;
		}
		throw new SQLException("Failed to insert row into " + url);
	}

	public int delete(Uri url, String where, String[] whereArgs) {
		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
		int count;
		String segment = "";
		switch (URL_MATCHER.match(url)) {
		case TODOS:
			count = mDB.delete(TABLE_NAME, where, whereArgs);
			break;
		case TODOS__ID:
			segment = url.getPathSegments().get(1);
			count = mDB.delete(TABLE_NAME,
					"_id="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_TITLE:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"title="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_IMPORTANCE:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"importance="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_DEADLINE:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"deadline="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_NOTES:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"notes="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_CREATED:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"created="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_FINISH:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"finish="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}

	public int update(Uri url, ContentValues values, String where,
			String[] whereArgs) {
		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
		int count;
		String segment = "";
		switch (URL_MATCHER.match(url)) {
		case TODOS:
			count = mDB.update(TABLE_NAME, values, where, whereArgs);
			break;
		case TODOS__ID:
			segment = url.getPathSegments().get(1);
			count = mDB.update(TABLE_NAME, values,
					"_id="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_TITLE:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"title="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_IMPORTANCE:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"importance="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_DEADLINE:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"deadline="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_NOTES:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"notes="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_CREATED:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"created="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case TODOS_FINISH:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"finish="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}

	static {
		URL_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase(), TODOS);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/#",
				TODOS__ID);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/title"
				+ "/*", TODOS_TITLE);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/importance"
				+ "/*", TODOS_IMPORTANCE);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/deadline"
				+ "/*", TODOS_DEADLINE);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/notes"
				+ "/*", TODOS_NOTES);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/created"
				+ "/*", TODOS_CREATED);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/finish"
				+ "/*", TODOS_FINISH);

		TODOS_PROJECTION_MAP = new HashMap<String, String>();
		TODOS_PROJECTION_MAP.put(_ID, "_id");
		TODOS_PROJECTION_MAP.put(TITLE, "title");
		TODOS_PROJECTION_MAP.put(IMPORTANCE, "importance");
		TODOS_PROJECTION_MAP.put(DEADLINE, "deadline");
		TODOS_PROJECTION_MAP.put(NOTES, "notes");
		TODOS_PROJECTION_MAP.put(CREATED, "created");
		TODOS_PROJECTION_MAP.put(FINISH, "finish");

	}
}
